﻿using Deployment.Data.Environment;
using Deployment.Data.UI.Dialogs.Environment;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deployment.Data.UI.Controls.Environment
{
    /// <summary>
    /// Interaction logic for EnvironmentEditableList.xaml
    /// </summary>
    public partial class EnvironmentEditableList : UserControl
    {
        public static readonly DependencyProperty SelectedEnvironmentProperty =
            DependencyProperty.Register("SelectedEnvironment",
                typeof(Data.Environment.Environment),
                typeof(EnvironmentEditableList), null);

        public event DependencyPropertyChangedEventHandler PropertyChanged;
        private void SetValueDp(DependencyProperty property, object value)
        {
            SetValue(property, value);
            if (PropertyChanged != null)
                PropertyChanged(this, new DependencyPropertyChangedEventArgs());
        }

        public EnvironmentEditableList()
        {
            InitializeComponent();
        }
        
        public Data.Environment.Environment SelectedEnvironment
        {
            get { return (Data.Environment.Environment)GetValue(SelectedEnvironmentProperty); }
            set { SetValueDp(SelectedEnvironmentProperty, value); }
        }

        private void AddEnvironement_Click(object sender, RoutedEventArgs e)
        {
            EnvironmentsEditorControl parentControl = WPFHelpers.TryFindParent<EnvironmentsEditorControl>(this);
            AddEnvironmentDialog dialog = new AddEnvironmentDialog((DataContext as EnvironmentsFile).Environments);
            dialog.Owner = Window.GetWindow(this);
            bool? result = dialog.ShowDialog();
            if (result.HasValue && result.Value == true)
            {
                Data.Environment.Environment newEnv = dialog.newEnvironment;
                (DataContext as EnvironmentsFile).AddEnvironment(newEnv);
                parentControl.HasUnsavedChanges = true;
            }
        }

        private void RemoveEnvironment_Click(object sender, RoutedEventArgs e)
        {
            EnvironmentsEditorControl parentControl = WPFHelpers.TryFindParent<EnvironmentsEditorControl>(this);
            EnvironmentsFile envFile = parentControl.DataContext as EnvironmentsFile;
            if (envFile != null)
            {
                Data.Environment.Environment env = lsv_Environments.SelectedItem as Data.Environment.Environment;
                if (env != null)
                {
                    MessageBoxResult result = MessageBox.Show(Window.GetWindow(this),
                        string.Format("Are you sure you want to remove Environment '{0}' from the Environment list?", env.Name),
                        "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        envFile.RemoveEnvironment(env);
                        parentControl.HasUnsavedChanges = true;
                    }
                }
            }
        }

        private void CloneEnvironment_Click(object sender, RoutedEventArgs e)
        {
            EnvironmentsEditorControl parentControl = WPFHelpers.TryFindParent<EnvironmentsEditorControl>(this);
            CloneEnvironmentDialog dialog = new CloneEnvironmentDialog(lsv_Environments.SelectedItem as Data.Environment.Environment,
                (DataContext as EnvironmentsFile).Environments);
            dialog.Owner = Window.GetWindow(this);
            bool? result = dialog.ShowDialog();
            if (result.HasValue && result == true)
            {
                (DataContext as EnvironmentsFile).CloneEnvironment((lsv_Environments.SelectedItem as Data.Environment.Environment).Name,
                    dialog.NewName, true);
                parentControl.HasUnsavedChanges = true;
            }
        }

        private void lsv_Environments_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = lsv_Environments.SelectedItem as Data.Environment.Environment;
            SelectedEnvironment = item;
        }
    }
}
